<!--
Copyright 2014 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="../../lib/log.html">
<link rel="import" href="../../lib/network-simulator.html">
<link rel="import" href="../cqstats-graph-data.html">

<script>
(function() {

var assert = chai.assert;

var testCQStats = {
  results: [{
    key: '2002',
    begin: 1500,
    end: 1500 + 500 * 60,
    interval_minutes: 500,
    stats: [{
      name: 'test-stat-a',
      max: 600,
      percentile_99: 540,
      percentile_90: 480,
      percentile_50: 420,
      min: 360,
      mean: 420,
    }, {
      name: 'test-stat-b',
      max: 6000,
      percentile_99: 5400,
      percentile_90: 4800,
      percentile_50: 4200,
      min: 3600,
      mean: 4200,
    }],
  }, {
    key: '1001',
    begin: 1000,
    end: 1000 + 500 * 60,
    interval_minutes: 500,
    stats: [{
      name: 'test-stat-a',
      max: 6000,
      percentile_99: 5400,
      percentile_90: 4800,
      percentile_50: 4200,
      min: 3600,
      mean: 4200,
    }, {
      name: 'test-stat-b',
      max: 600,
      percentile_99: 540,
      percentile_90: 480,
      percentile_50: 420,
      min: 360,
      mean: 420,
    }],
  }],
};

describe('CQStatsGraphData.createBatch', function() {
  it('should create CQStatsGraphData objects that get data', function(done) {
    var simulator = new NetworkSimulator(assert);
    simulator.json = function(options) {
      if (options.url === 'https://chromium-cq-status.appspot.com/stats/query?project=testProject&interval_minutes=500&names=test-stat-a%2Ctest-stat-b&count=1000') {
        return Promise.resolve(testCQStats);
      }
      console.log('Unexpected url: ' + options.url);
    }

    var dataList;
    simulator.runTest(function() {
      dataList = CQStatsGraphData.createBatch(
          'testProject', 500, ['test-stat-a', 'test-stat-b'], 1000);
    }).then(function() {
      assert.equal(dataList.length, 2);
      assert.equal(dataList[0]._name, 'test-stat-a');
      assert.equal(dataList[1]._name, 'test-stat-b');
      return Promise.all([
        dataList[0].get().then(function(data) {
          assert.deepEqual(data, {
            cols: ['timestamp', 'max', 'p90', 'p50', 'min', 'mean'],
            rows: [
              [new Date((1000 + 500 * 60) * 1000), 100, 80, 70, 60, 70],
              [new Date((1500 + 500 * 60) * 1000), 10, 8, 7, 6, 7],
            ],
          });
        }),
        dataList[1].get().then(function(data) {
          assert.deepEqual(data, {
            cols: ['timestamp', 'max', 'p90', 'p50', 'min', 'mean'],
            rows: [
              [new Date((1000 + 500 * 60) * 1000), 10, 8, 7, 6, 7],
              [new Date((1500 + 500 * 60) * 1000), 100, 80, 70, 60, 70],
            ],
          });
        }),
      ]);
    }).then(function() {
      done();
    }).catch(log);
  });
});

})();
</script>

